package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDsihMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    SetmealDsihMapper setmealDsihMapper;

    /**
     * 新增套餐
     */
    @Override
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        //将Dto 转换成普通属性实用 sprig提供的工具
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.inset(setmeal);
        //关联菜品
        Long SetmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(SetmealId);
            });
        }
        //向关联的菜品表插入n条数据
        setmealDsihMapper.insertBatch(setmealDishes);
    }


    @Override
    public PageResult PageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //实用分页插件
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        //查询套餐
        Page<SetmealVO> setmealVOS = setmealMapper.pageQuery(setmealPageQueryDTO);

        //记录条数
        Long total = setmealVOS.getTotal();
        List<SetmealVO> result = setmealVOS.getResult();


        return new PageResult(total, result);
    }


    /**
     * 删除套餐
     *
     * @param ids
     */
    @Override
    public void Delete(List<Long> ids) {
        //先判断是否在起售中
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getByid(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                //起售中不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        //先删除包含的菜品
        setmealDsihMapper.deleteByids(ids);

        //删除套餐
        setmealMapper.deleteByids(ids);
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO getByIdWithDish(Long id) {
        //查询出套餐
        SetmealVO setmealVO= setmealMapper.getByIdWithDish(id);
        //查询套餐包含的菜品
//        List<SetmealDish> setmealDishes = setmealDsihMapper.getByid(id);
//
//        //创建包含菜品的套餐类型
//        SetmealVO setmealVO = new SetmealVO();
//        BeanUtils.copyProperties(setmeal, setmealVO);
//        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     */
    @Override
    public void Update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        //修改套餐信息
        setmealMapper.Update(setmeal);

        //删除包含菜品
        setmealDsihMapper.deleteByid(setmealDTO.getId());

        //插入新的包含菜品信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDTO.getId());//给菜品附上套餐id
        });
        setmealDsihMapper.insertBatch(setmealDishes);
    }

    /**
     * 跟新套餐状态
     * @param status
     */
    @Override
    public void UpdateStatus(Integer status, Long id) {

        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.Update(setmeal);

    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
